<template>
  <div class="app-container">
    <div class="filter-container" style="padding-bottom:20px;">
      <el-input v-model="listQuery.name" placeholder="名称 封装 项目 产品" style="width: 400px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-date-picker
        v-model="listQuery.datetimeStart"
        type="datetime"
        placeholder="选择日期时间">
      </el-date-picker>
      <span class="demonstration">-</span>
      <el-date-picker
        v-model="listQuery.datetimeEnd"
        type="datetime"
        placeholder="选择日期时间">
      </el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-document">
        导出excel
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
      style="width: 100%;"
      >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="名字" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="权限" width="410" align="center">
        <template slot-scope="scope">
          <el-tag
            v-for="tag in scope.row.roles"
            :key="tag"
            >
            {{tag}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="登录名" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.login_name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button @click="handleModify(scope.row)" type="text" size="small">修改权限</el-button>
          <el-button @click="handleModifyUpdate(scope.row)" type="text" size="small">更新权限</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="权限修改" width="50%" :visible.sync="dialogModifyVisable">
      <el-tag
        :key="tag"
        v-for="tag in dynamicRoles"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{tag}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
        >
      </el-input>
      <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 添加权限</el-button>
    </el-dialog>
  </div>
</template>


<script>
import { userList, userRoleModify } from '@/api/user'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Permission',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      total: 10,
      listQuery: {
        name: null,
        page: 1,
        limit: 20,
        datetimeStart: '',
        datetimeEnd: ''
      },
      dialogModifyVisable: false,
      dynamicRoles: null,
      inputVisible: false,
      inputValue: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      userList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleModify(row) {
      this.dynamicRoles = row.roles
      this.dialogModifyVisable = true
    },
    handleModifyUpdate(row) {
      userRoleModify(row.login_name, JSON.stringify(row.roles)).then(response => {
        this.$message({
          type: 'success',
          message: response.message
        })
        this.handleFilter()
      })
    },
    handleClose(tag) {
      this.dynamicRoles.splice(this.dynamicRoles.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicRoles.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

